﻿#include "stdafx.h"
#include "skinspin.h"
#include "SpinSkin.h"

CSkinSpin::CSkinSpin(void)
{
	//m_pSpinSkin = GetSkin().GetSpinSkin();
	m_nHotButton = UNKNOW;
	m_nPressedButton = UNKNOW;
}

CSkinSpin::~CSkinSpin(void)
{
	HookWindow( (HWND)NULL);
}
BOOL CSkinSpin::InstallSkin(HWND hWnd)
{
    if (!hWnd || !LoadSkin())
    {
        return FALSE;
    }
	HookWindow( (HWND)NULL);

	int r = HookWindow( hWnd );


	m_pSpinSkin = GetSkin().GetSpinSkin();
	if(m_pSpinSkin == NULL)
		m_bEnableSkin = FALSE;

#ifdef _WIN64
	LONG_PTR style = GetWindowLongPtr(m_hWnd, GWL_STYLE);
	LONG_PTR exstyle = GetWindowLongPtr(m_hWnd, GWL_EXSTYLE);
#else
	DWORD style = GetWindowLong(m_hWnd, GWL_STYLE);
	DWORD exstyle = GetWindowLong(m_hWnd, GWL_EXSTYLE);
#endif
	if((style & UDS_HORZ)  == UDS_HORZ)
		m_bVert = FALSE;
	else
		m_bVert = TRUE;

	m_nHotButton = UNKNOW;
	m_nPressedButton = UNKNOW;
	SetWindowPos(m_hWnd,NULL, 0, 0, 0, 0,  SWP_FRAMECHANGED|SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER); 
    return TRUE;
}
BOOL CSkinSpin::LoadSkin()
{
    CAutoEnableSkin a(m_bEnableSkin);
	m_pSpinSkin = GetSkin().GetSpinSkin();
    if (!m_pSpinSkin)
    {
        return FALSE;
    }
	SetWindowPos(m_hWnd,NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER); 
    return TRUE;
}
LRESULT CSkinSpin::OnWndMsg(UINT msg,WPARAM wp,LPARAM lp)
{
	switch(msg)
	{
	case WM_MOUSELEAVE:
		OnMouseLeave();
		return 0;
	}
	return CSkinWnd::OnWndMsg(msg,wp,lp);
}
void CSkinSpin::OnPaint()
{
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
	CPaintDC dc(pSpin);
	DrawSpin(&dc);

}
BOOL CSkinSpin::OnEraseBkgnd(CDC *pDC)
{
	return TRUE;
}

void CSkinSpin::OnMouseMove( UINT nFlags, const CPoint& point )
{
	//Default();
	if((m_nHotButton == UNKNOW) && ((nFlags & MK_LBUTTON) == 0) )
	{
		TRACKMOUSEEVENT tme;
		m_nHotButton = HitTest(point);
		m_nPressedButton = UNKNOW;
		tme.cbSize = sizeof(TRACKMOUSEEVENT);
		tme.dwFlags = TME_LEAVE;
		tme.hwndTrack = m_hWnd;
		TrackMouseEvent(&tme);
	}
	else if(nFlags & MK_LBUTTON)
	{
		TRACKMOUSEEVENT tme;
		m_nPressedButton = HitTest(point);
		m_nHotButton = UNKNOW;
		tme.cbSize = sizeof(TRACKMOUSEEVENT);
		tme.dwFlags = TME_LEAVE;
		tme.hwndTrack = m_hWnd;
		TrackMouseEvent(&tme);
	}
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(reinterpret_cast<CWnd*>(pSpin));
	pDC = pSpin->GetDC();
	DrawSpin(pDC);
	
}
void CSkinSpin::OnLButtonDown( UINT nFlags, const CPoint& point )
{
	Default();
	m_nPressedButton = HitTest(point);
	m_nHotButton = UNKNOW;
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(reinterpret_cast<CWnd*>(pSpin));
	pDC = pSpin->GetDC();
	DrawSpin(pDC);
}
void CSkinSpin::OnMouseLeave()
{
	if(m_nHotButton != UNKNOW)
		m_nHotButton = UNKNOW;
	if(m_nPressedButton != UNKNOW)
		m_nPressedButton = UNKNOW;
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(reinterpret_cast<CWnd*>(pSpin));
	pDC = pSpin->GetDC();
	DrawSpin(pDC);
}
void CSkinSpin::OnLButtonUp( UINT nFlags, const CPoint& point )
{
	Default();
	m_nPressedButton = UNKNOW;
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
    CAutoReleaseDC pDC(reinterpret_cast<CWnd*>(pSpin));
	pDC = pSpin->GetDC();
	DrawSpin(pDC);
	
}
UINT CSkinSpin::HitTest( const CPoint& point )
{
	CRect rtClient;
	GetClientRect(rtClient);
	CRect rtUp,rtDown;
	rtUp = rtClient;
	rtDown = rtClient;
	if(m_bVert)
	{
		rtUp.bottom = (rtClient.top + rtClient.bottom)/2;
		rtDown.top = (rtClient.top + rtClient.bottom)/2;
	}
	else
	{
		rtUp.right = (rtClient.left + rtClient.right)/2;
		rtDown.left = (rtClient.left + rtClient.right)/2;
	}
	
	if(rtUp.PtInRect(point))
		return UP;
	if(rtDown.PtInRect(point))
		return DOWN;
	return UNKNOW;
}
void CSkinSpin::DrawSpin(CDC *pDC)
{
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(m_hWnd);
	CRect rtClient;
	GetClientRect(rtClient);
	rtClient.DeflateRect(2,0,1,0);

	CRect rtUp,rtDown;
	rtUp = rtClient;
	rtDown = rtClient;
	if(m_bVert)
	{
		rtUp.bottom = (rtClient.top + rtClient.bottom)/2;
		rtDown.top = (rtClient.top + rtClient.bottom)/2;

		if(m_nPressedButton == UP)
		{
			if(!m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinPressed].imageUp))
				m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinNormal].imageDown);
		}
		else if(m_nPressedButton == DOWN)
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinNormal].imageUp);
			if(!m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinPressed].imageDown))
				m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinNormal].imageDown);
		}
		else if(m_nHotButton == UP)
		{
			if(!m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinHover].imageUp))
				m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinNormal].imageDown);
		}
		else if(m_nHotButton == DOWN)
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinNormal].imageUp);
			if(!m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinHover].imageDown))
				m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinNormal].imageDown);
		}
		else
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbVert[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbVert[keSpinNormal].imageDown);
		}
	}
	else
	{
		rtUp.right = (rtClient.left + rtClient.right)/2;
		rtDown.left = (rtClient.left + rtClient.right)/2;

		if(m_nPressedButton == UP)
		{
			if(!m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinPressed].imageUp))
				m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinNormal].imageDown);
		}
		else if(m_nPressedButton == DOWN)
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinNormal].imageUp);
			if(!m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinPressed].imageDown))
				m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinNormal].imageDown);
		}
		else if(m_nHotButton == UP)
		{
			if(!m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinHover].imageUp))
				m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinNormal].imageDown);
		}
		else if(m_nHotButton == DOWN)
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinNormal].imageUp);
			if(m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinHover].imageDown))
				m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinNormal].imageDown);
		}
		else
		{
			m_pSpinSkin->DrawImageSection(pDC,rtUp,m_pSpinSkin->m_thumbHort[keSpinNormal].imageUp);
			m_pSpinSkin->DrawImageSection(pDC,rtDown,m_pSpinSkin->m_thumbHort[keSpinNormal].imageDown);
		}
	}
}